﻿<Settings>
  <setting name="JsonRules">
    <value>
      <![CDATA[
      {
        "RulesForEntities": [
          {
              "Key": "contact", "Value":
                {
                    "EntityName": "contact", "Rules": [
                      {
                          "Name": "Validate Contact.annualincome range is 1,000 - 200,000. On failure, set another field, send email and create task. Use Formula to calculate Credit Limit.",
                          "SourceSetting": {
                              "FieldName": "annualincome",
                              "FieldValidators": [
                              {
                                  "Name": "Validate entire operation against value in grandparent record: Primary Contact of Account associated with this Contact must have CreditHold=No. NOTE: this validator ONLY looks at a value in related record",
                                  "ValidatedFieldIsInRelatedRecord": "true",
								                  "ThrowExceptionWhenInvalid": "true",
                                  "Action": {
                                      "ActionKind": "SendEmail",
                                      "EntityLogicalName": "contact"
                                  },
                                  "Range": {
                                      "RangeValidationKind": "Equals",
                                      "ParentEntityFetchQuery": "<fetch distinct='false' mapping='logical' aggregate='false'> <entity name='contact'><attribute name='fullname' /> <filter type='and'><condition attribute='contactid' operator='eq' value='<PK_VALUE>' /> </filter> <link-entity name='account' to='parentcustomerid' from='accountid' alias='acc' link-type='inner'>  <attribute name='name'/><link-entity name='contact' to='primarycontactid' from='contactid' alias='grands' link-type='inner'>  <attribute name='fullname'/><attribute name='creditonhold'/></link-entity></link-entity></entity> </fetch>",
                                      "EqualsValue": {
                                          "CrmFieldType": "CrmBoolean",
                                          "Name": "grands.creditonhold",
                                          "Value": false
                                      }
                                  }
                              },
                              {
                                  "Name": "Validate annualincome against static Range ",
								                  "ThrowExceptionWhenInvalid": "false",
                                  "Action": {
                                      "ActionField": {
                                          "CrmFieldType": "CrmOptionset",
                                          "Name": "paymenttermscode",
                                          "Value": 3
                                      },
                                      "ActionKind": "FieldAndTaskAndEmail",
                                      "Entity": {"LogicalName": "contact"}
                                  },
                                  "Range": {
                                      "MaxValue": {
                                          "CrmFieldType": "CrmMoney",
                                          "Name": "annualincome",
                                          "Value": 200000
                                      },
                                      "MinValue": {
                                          "CrmFieldType": "CrmMoney",
                                          "Name": "annualincome",
                                          "Value": 1000
                                      },
                                      "RangeValidationKind": "Range"
                                  }
                              }],
                              "Formulas":[{
	                              "Name":"Set Credit Limit based on Gender and Annual Income",
	                              "ActionType":0,
	                              "Expression":"if ( [gendercode] == 1, [annualincome] * 1.5, [annualincome] * 0.7)",
	                              "FailOnMissingField":false,
	                              "OutputCrmField":{
		                              "CrmFieldType":"CrmMoney",
		                              "Name":"creditlimit",
		                              "Value":0}
	                              }],
                              "RollupOperation": { "OperationType": "NoOp" }
                          }
                      }]
                }
          }]
    }
      ]]>
    </value>
  </setting>
</Settings>